/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.at_commands;

public abstract class AbstractAtCommand implements AtCommand
{
	private final  String command;
	private int  sequenceNbr;
	private  StringBuilder builder;
	private boolean ready;
	
	public AbstractAtCommand(String command)
	{
		this.builder = new StringBuilder();
		this.command = builder.append(PREFIX).append(command).append('=').toString();
		this.ready = false;
	}
	
	public void sequenceNbr(int sequenceNbr)
	{
		this.ready = false;
		this.sequenceNbr = sequenceNbr;
	}
	
	protected abstract void appendParamaters(StringBuilder builder);

	public boolean ready()
	{
		return this.ready;
	}
	
	public void unReady()
	{
		 this.ready = false;
	}
	
	private  void build()
	{
		this.ready = true;
		this.builder.delete(0, this.builder.length());
		this.builder.append(command).append(this.sequenceNbr);
		this.appendParamaters(this.builder);
		this.builder.append("\r");
	}

	public byte[] bytes()
	{
		return this.toString().getBytes();
	}

	public final  String toString()
	{
		if (! ready) this.build();
		return this.builder.toString();
	}
	
}
